<template>
  <page-layout class="task">
    <div class="search-wrapper">
      <div>
        <a-input
          placeholder="请输入任务名称"
          class="search-input"
          v-model="searchText"
        >
          <a-icon
            slot="prefix"
            type="search"
            style="font-size: 28px; color: #999"
          />
        </a-input>
        <a-button type="primary" class="search-button" @click="handelsearch"
          >搜索</a-button
        >
      </div>
      <a-button
        style="width: 120px; height: 54px"
        type="primary"
        @click="
          $router.push({
            path: '/task/add',
            query: { random: random }
          })
        "
      >
        新增
      </a-button>
    </div>

    <!-- <a-card> -->
    <div class="tabel-wrapper">

      <standard-table
        :columns="columns"
        :dataSource="dataSource"
        @change="onChange"
        :pagination="pagination"
        size="large"
        :loading="loading"
      >
       <div slot="truly_time" slot-scope="{text,record }">
          <template >
           <template v-if="text != null" >
            <a style="text-decoration:underline;" @click="log_info(record.key)"  title='点击查看任务日志'>
             {{text}}
            </a>
          </template>
          </template>
         
        </div>
        <div slot="status" slot-scope="{ text }">
          <template v-if="text == 1">
            <a-tag
              style="
                width: 70px;
                height: 30px;
                text-align: center;
                line-height: 30px;
              "
              color="#2591FF"
            >
              待开始
            </a-tag>
          </template>
          <template v-else-if="text == 2">
            <a-tag
              style="
                width: 70px;
                height: 30px;
                text-align: center;
                line-height: 30px;
              "
              color="#FF6D25"
            >
              进行中
            </a-tag>
          </template>
          <template v-else>
            <a-tag
              style="
                width: 70px;
                height: 30px;
                text-align: center;
                line-height: 30px;
              "
              color="#4E25FF"
            >
              测试验收
            </a-tag>
          </template>
        </div>
        <!-- eslint-disable-next-line -->
        <div slot="action" slot-scope="{ text, record }">
          <!-- {{ record.key }} -->
          <a
            :href="`#/task/detail?random=${random}&&id=${record.key}`"
            style="margin-right: 8px"
            >查看 </a
          >
          <!-- <a
            :href="`#/task/add?random=${random}&&id=${record.key}`"
            style="margin-right: 8px"
            >编辑 |</a
          > -->

          <!-- <a-popconfirm
            title="确定删除此条需求吗?"
            ok-text="是的"
            cancel-text="取消"
            @confirm="() => confirm(record.key)"
            @cancel="() => cancel(record.key)"
          >
            <a>删除</a>
          </a-popconfirm> -->
        </div>
        <template slot="statusTitle">
          <a-icon @click.native="onStatusTitleClick" type="info-circle" />
        </template>
      </standard-table>
    </div>
      <a-modal v-model="log_visible" title="任务日志" ok-text="确认"  @ok="hideModal">
              <a-table :columns="log_columns" :data-source="log_data">
              <a slot="name" slot-scope="">{{ text }}</a>
              </a-table>
          </a-modal>
    <!-- </a-card> -->
  </page-layout>
</template>

<script>
import PageLayout from "@/layouts/PageLayout";
import StandardTable from "@/components/table/StandardTable";
import { list, deleteItem, logs} from "@/services/task";
// import { mapMutations } from "vuex";
const columns = [
   {
    title: "ID",
    dataIndex: "key"
  },
   {
    title: "关联需求",
    dataIndex: "requirement_name"
  },
  {
    title: "任务名称",
    dataIndex: "title"
  },
  {
    title: "工程师",
    dataIndex: "engineer_name"
  },
  {
    title: "对接人",
    dataIndex: "docking_name",
  },
  {
    title: "总耗时",
    dataIndex: "truly_time",
     scopedSlots: { customRender: "truly_time" }
  },
  // {
  //   title: "任务状态",
  //   dataIndex: "status",
  //   scopedSlots: { customRender: "status" }
  // },
  {
    title: "操作",
    scopedSlots: { customRender: "action" },
    align: "center"
  }
];
const log_columns = [

  {
    title: 'id',
    dataIndex: 'id',
    key: 'id',
      },
    {
    title: '开始时间',
    dataIndex: 'created_at',
    key: 'created_at',
    ellipsis: true,
  },
  {
    title: '备注内容',
    dataIndex: 'desc',
    key: 'desc',
    width: 120,
  },
    {
    title: '耗时',
    dataIndex: 'hours',
    key: 'hours',
    width: 120,
  },
  {
    title: '结束时间',
    dataIndex: 'end_at',
    key: 'end_at',
    ellipsis: true,
  },
];

export default {
  name: "demand",
  components: { PageLayout, StandardTable },
  data() {
    return {
      log_columns ,
      log_visible:false ,
      log_data:"" ,
      searchText: "",
      random: "",
      loading: true,
      desc: "需求管理中心",
      columns: columns,
      dataSource: [],
      selectedRows: [],
      pagination: {
        size: "small",
        showSizeChanger: true,
        showQuickJumper: true,
        poposition: "bottom",
        pageSize: 5,
        current: 1,
        total: 0
      }
    };
  },
  authorize: {
    deleteRecord: "delete"
  },
  activated() {
    const { pageSize, current } = this.pagination;
    this.random = Math.random();
    this.loadTableData({ limit: pageSize, page: current, status: 6 });
  },
  created() {
    // 初始数据
    const { pageSize, current } = this.pagination;
    this.random = Math.random();
    this.loadTableData({ limit: pageSize, page: current, status: 6 });
  },
  methods: {
     hideModal() {
      this.log_visible = false;
    },
      log_info(id) {
         this.log_visible = true;
         logs(id).then(
            (res) => {
              console.log(res.data.data);
              if (res.data.code === 20000) {
                this.log_data = res.data.data ;
                  // this.$message.success(res.data.message);
              }
            }
          ); 
    },
    // 请求table 的数据
    // obj 请求的字段
    loadTableData(obj) {
      this.loading = true;
      list(obj).then((res) => {
        // console.log(res.data, 222222);
        // 关闭loading
        this.loading = false;
        if (res.data.code === 20000) {
          const { current_page, total, per_page } = res.data.data;
          Object.assign(this.pagination, {
            total: parseInt(total),
            current: parseInt(current_page),
            pageSize: parseInt(per_page)
          });
          this.dataSource = res.data.data.data.map((item) => {
            // console.log(item);
            const {
              requirement_name ,
              task_id,
              created_at,
              title,
              engineer_name,
              docking_name,
              truly_time
            } = item;
            return {
              requirement_name ,
              key: task_id,
              created_at: created_at && created_at.split(" ")[0],
              title: title || "未知",
              engineer_name: engineer_name || "未知",
              docking_name: docking_name || "未知",
              truly_time
            };
          });
        }
      });
    },
    handelsearch() {
      const { pageSize, current } = this.pagination;
      this.loadTableData({
        limit: pageSize,
        page: current,
        title: this.searchText
      });
      this.searchText = "";
    },
    deleteRecord(key) {
      this.dataSource = this.dataSource.filter((item) => item.key !== key);
      this.selectedRows = this.selectedRows.filter((item) => item.key !== key);
    },
    onClear() {
      this.$message.info("您清空了勾选的所有行");
    },
    onStatusTitleClick() {
      this.$message.info("你点击了状态栏表头");
    },
    onChange(e) {
      const { current, pageSize } = e;
      this.loadTableData({ limit: pageSize, page: current, status: 6 });
    },
    handleMenuClick(e) {
      if (e.key === "delete") {
        this.remove();
      }
    },
    cancel(key) {
      this.$message.info(`点击了取消,行数为${key + 1}行`);
    },
    confirm(key) {
      // 发起删除的请求
      deleteItem(key).then((res) => {
        // console.log(res, "delete");
        // 成功
        if (res.data.code === 20000) {
          // 提示信息
          this.$message.success({
            content: res.data.message || "删除成功~"
          });
          // 删除成功后重新获取数据
          const { pageSize, current } = this.pagination;
          this.loadTableData({ limit: pageSize, page: current, status: 6 });
        } else {
          this.$message.error(res.data.message || "删除失败！");
        }
      });
      // this.$message.info(`点击了确定,行数为${key + 1}行`);
      // this.deleteRecord(key);
    }
  }
};
</script>

<style lang="less">
.search-wrapper {
  display: flex;
  justify-content: space-between;
  margin-bottom: 30px;

  > div {
    .search-input input {
      padding-left: 45px !important;
    }
    display: flex;
    width: 60%;
    .ant-input-affix-wrapper {
      display: inline-block;
      width: calc(100% - 150px);
      height: 54px !important;
      input {
        height: 100% !important;
      }
      input::-webkit-input-placeholder {
        color: #9999;
        font-size: 16px;
      }

      input::-moz-placeholder {
        /* Mozilla Firefox 19+ */
        color: #9999;
        font-size: 16px;
      }

      input:-moz-placeholder {
        /* Mozilla Firefox 4 to 18 */
        color: #9999;
        font-size: 16px;

        font-size: 17px;
      }

      input:-ms-input-placeholder {
        /* Internet Explorer 10-11 */
        color: #9999;
        font-size: 16px;
      }
    }
    .search-button {
      margin-left: 30px;
      width: 120px;
      height: 54px;
    }
  }
}
.task {
  .tabel-wrapper {
    border-radius: 5px;
    // min-height: 450px;
    background-color: #fff;
    color: #1f1f1f;
    .ant-pagination {
      margin-top: 60px !important;
    }
    tr {
      height: 66px !important;
      // padding-left: 10px;
    }
    .ant-table-thead {
      padding-left: 20px;
      background: #effaff;
      & > tr > th {
        background: #effaff !important;
      }
    }
    .ant-table-tbody {
      padding-left: 20px;
    }
    .ant-table-pagination.ant-pagination {
      float: unset !important;
      text-align: center;
    }
  }
}
</style>
